/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.List;
import model.Voorstelling;
import model.Zetel;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Matthy & Jelle
 */
public class ZetelService extends AbstractService{
  public List<Zetel> getVrijeZetels(Voorstelling voorstelling) {
    List<Zetel> zetels;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Query query = session.createQuery(
      "from Zetel z "
      + "where z.zaalzone.zaal.id = "
      + "(select v.zaal.id from Voorstelling v where v.id = :voorstellingid)"
      + " and z.id not in "
      + "(select ticket.zetel.id from Ticket ticket where ticket.voorstelling.id = :voorstellingid2)"
    );
    query.setInteger("voorstellingid", voorstelling.getId());
    query.setInteger("voorstellingid2", voorstelling.getId());
    zetels = query.list();
    session.getTransaction().commit();
    return zetels;
  }

  @Override
  public boolean validate(Object o) {
    Zetel z = (Zetel)o;
    return (z.getNr() != null && z.getNr() > 0 && z.getZaalzone() != null);
  }
}
